<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82882996  15899929162
// +----------------------------------------------------------------------
namespace app\common\model\property\bill;

use app\common\model\BaseModel;
use app\common\model\bill\BillCateModel;
use app\common\model\bill\BillFinanceModel;
use app\common\model\energy\EnergyBillModel;
use app\common\model\property\basic\PropertyBasicModel;

class PropertyBillMonthModel extends BaseModel
{
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    public static function tablePk(): string
    {
        return 'id';
    }

    public static function tableName(): string
    {
        return 'property_bill_month';
    }

    public function propertyInfo()
    {
        return $this->hasOne(PropertyBasicModel::class, 'id', 'property_id');
    }

    /**
     * 电费总额
     * @return float
     *
     * @author Amos
     * 2024-04-30 17:30
     */
    public function getElectricAmountAttr()
    {
        return EnergyBillModel::where('property_id', $this->property_id)
            ->where('month_time', $this->month_time)
            ->where('energy_type', 11)
            ->sum(\think\facade\Db::raw('basic_amount+service_amount+loss_amount'));
    }

    /**
     * 冷水费
     * @return float
     *
     * @author Amos
     * 2024-04-30 17:45
     */
    public function getColdWaterAmountAttr()
    {
        return EnergyBillModel::where('property_id', $this->property_id)
            ->where('month_time', $this->month_time)
            ->where('energy_type', 12)
            ->sum(\think\facade\Db::raw('basic_amount+service_amount+loss_amount'));
    }

    /**
     * 冷水费
     * @return float
     *
     * @author Amos
     * 2024-04-30 17:45
     */
    public function getHotWaterAmountAttr()
    {
        return EnergyBillModel::where('property_id', $this->property_id)
            ->where('month_time', $this->month_time)
            ->where('energy_type', 13)
            ->sum(\think\facade\Db::raw('basic_amount+service_amount+loss_amount'));
    }

    /**
     * 燃气费用
     * @return float
     *
     * @author Amos
     * 2024-04-30 17:45
     */
    public function getFuelGasAmountAttr()
    {
        return EnergyBillModel::where('property_id', $this->property_id)
            ->where('month_time', $this->month_time)
            ->where('energy_type', 14)
            ->sum(\think\facade\Db::raw('basic_amount+service_amount+loss_amount'));
    }

    /**
     * 水电气总费用
     * @return float
     *
     * @author Amos
     * 2024-04-30 17:46
     */
    public function getEnergyAmountAttr()
    {
        return EnergyBillModel::where('property_id', $this->property_id)
            ->where('month_time', $this->month_time)
            ->sum(\think\facade\Db::raw('basic_amount+service_amount+loss_amount'));
    }


    /**
     * 租金收益
     * @return string|null
     *
     * @author Amos
     * 2024-05-04 15:39
     */
    public function getClosedRentAttr()
    {
        $cateId = BillCateModel::where('is_class', 1)
            ->where('set_finance', 4)
            ->where('items_id', $this->items_id)
            ->value('id');
        return BillFinanceModel::where('property_id', $this->property_id)
            ->where('is_type', 1)
            ->where('cate_id', $cateId)
            ->where('month_time', $this->month_time)
            ->sum('amount');
    }

    /**
     * 押金收益
     * @return string|null
     *
     * @author Amos
     * 2024-05-04 15:39
     */
    public function getClosedDepositAttr()
    {
        $cateId = BillCateModel::where('is_class', 1)
            ->where('set_finance', 3)
            ->where('items_id', $this->items_id)
            ->value('id');
        return BillFinanceModel::where('property_id', $this->property_id)
            ->where('is_type', 1)
            ->where('cate_id', $cateId)
            ->where('month_time', $this->month_time)
            ->sum('amount');
    }

    /**
     * 租金支出
     * @return string|null
     *
     * @author Amos
     * 2024-05-04 15:39
     */
    public function getPayRentAttr()
    {
        $cateId = BillCateModel::where('is_class', 1)
            ->where('set_finance', 4)
            ->where('items_id', $this->items_id)
            ->value('id');
        return BillFinanceModel::where('property_id', $this->property_id)
            ->where('is_type', 2)
            ->where('cate_id', $cateId)
            ->where('month_time', $this->month_time)
            ->sum('amount');
    }

    /**
     * 押金支出
     * @return string|null
     *
     * @author Amos
     * 2024-05-04 15:39
     */
    public function getPayDepositAttr()
    {
        $cateId = BillCateModel::where('is_class', 1)
            ->where('set_finance', 3)
            ->where('items_id', $this->items_id)
            ->value('id');
        return BillFinanceModel::where('property_id', $this->property_id)
            ->where('is_type', 2)
            ->where('cate_id', $cateId)
            ->where('month_time', $this->month_time)
            ->sum('amount');
    }

    /**
     * 水电气支出
     * @return string|null
     *
     * @author Amos
     * 2024-05-04 15:39
     */
    public function getPayEnergyAttr()
    {
        $cateId = BillCateModel::where('is_class', 2)
            ->where('set_finance', 10)
            ->where('items_id', $this->items_id)
            ->value('id');
        return BillFinanceModel::where('property_id', $this->property_id)
            ->where('is_type', 2)
            ->where('cate_id', $cateId)
            ->where('month_time', $this->month_time)
            ->sum('amount');
    }

    /**
     * 当月收益率
     * @return string|null
     *
     * @author Amos
     * 2024-05-04 15:39
     */
    public function getYieldRateAttr()
    {
        ## 收益率   = (租金收益 - 租金支出) / 租金支出  * 100%
        if ($this->pay_rent > 0) {
            return bcdiv(bcsub($this->closed_rent, $this->pay_rent, 2), $this->pay_rent, 2);
        } else {
            return '--';
        }

    }


}
